
/**
 * $loopWithDelay(()=>{
 *  return 'break';
 * },20,2)
 * @param callback 回调函数 返回break字符串结束循环
 * @param delay 时间 毫秒
 * @param count 次数 -1为永久
 */
export const $loopWithDelay = async (
  callback: (iteration: number) => Promise<'break'> | void | 'break',
  delay = 0,
  count = -1
) => {
  for (let i = 0; count === -1 ? true : i < count; i++) {
    if (!callback) continue

    const shouldBreak = await callback(i)
    if (shouldBreak === 'break') break

    if (delay > 0) {
      await new Promise(resolve => setTimeout(resolve, delay))
    }
  }
}
